package com.woniuxy.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniuxy.common.Result;
import com.woniuxy.common.StatusCode;
import com.woniuxy.domain.User;
import com.woniuxy.dto.DtoMyPage;
import com.woniuxy.dto.DtoUserInfo;
import com.woniuxy.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.beanutils.PropertyUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;

/**
 * @ClassName UserInfoController
 * @Description 用户管理系统
 * @Author Zcd
 * @Version 1.0
 **/

@RestController
@RequestMapping("/userInfo")
@Api(tags = "用户管理系统")
public class UserInfoController {
    @Resource
    private UserService userService;

    @GetMapping("allUsr")
    @ApiOperation("获取所有用户信息")
    /**
     * @Author： Zcd
     * @Description: 获取所有用户信息
     * @Param: [page]
     * @Return: com.woniuxy.common.Result
     */
    public Result getAllUsr(int current, int size) throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        DtoMyPage<DtoUserInfo> allUsers = userService.selectAllUsers(current, size);
        return new Result(true, StatusCode.OK, "查询成功", allUsers);
    }

    @GetMapping("/searchUsr")
    @ApiOperation("模糊查询用户信息")
    /**
    * @Author： Zcd
    * @Description: 模糊查询用户信息
    * @Param: [info, current, size]
    * @Return: com.woniuxy.common.Result
    */
    public Result getUsrByInfo(String info, int current, int size) throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        DtoMyPage<DtoUserInfo> dtoMyPage = userService.selectUsrByInfo(info, current, size);
        return new Result(true, StatusCode.OK, "查询成功", dtoMyPage);
    }

    @DeleteMapping("dropUsr")
    @ApiOperation("删除用户")
    /**
     * @Author： Zcd
     * @Description: 删除用户
     * @Param: [userId]
     * @Return: com.woniuxy.common.Result
     */
    public Result dropUsr(int userId) {
        userService.removeById(userId);
        return new Result(true, StatusCode.OK, "删除成功");
    }

    @PutMapping("updateUsr")
    @ApiOperation("修改用户信息")
    /**
     * @Author： Zcd
     * @Description: 修改用户信息
     * @Param: [DtoUserInfo]
     * @Return: com.woniuxy.common.Result
     */
    public Result updateUsr(@RequestBody DtoUserInfo dtoUserInfo) {
        boolean isSuccess = userService.updateUsr(dtoUserInfo.getUserId(), dtoUserInfo.getAccount(), dtoUserInfo.getUserName(), dtoUserInfo.getSex(), dtoUserInfo.getTel(), dtoUserInfo.getEMail());
        if (isSuccess) {
            return new Result(true, StatusCode.OK, "修改成功");
        } else {
            return new Result(false, StatusCode.ERROR, "修改失败");
        }
    }


}
